/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuComprarPeixePiscina.java
 *
 * Created on 21/09/2009, 10:27:38
 */

package JPaneis;

import Conector.Conector;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import siii.MenuPadrao;


public class MenuComprarPeixePiscina_1 extends javax.swing.JPanel {

    private MenuPadrao menuPadrao;
    private Conector conector;
    private static final String numServ = "NUMERO_SERVICO";
    List<Map<String,?>> listaPeixes;
    private float val;
    private float peso;
    private float valorTotal;
    private ImageIcon iconeOK;
    private ImageIcon iconeERRO;


    /** Creates new form MenuComprarPeixePiscina */
    public MenuComprarPeixePiscina_1(MenuPadrao m, Conector con) {
        this.menuPadrao = m;
        this.conector = con;
        initComponents();
        refreshList();
        iconeOK = new ImageIcon(getClass().getResource("/Images/fish-48x48ok.png"));
        iconeERRO = new ImageIcon(getClass().getResource("/Images/fish-48x48erro.png"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        compraPiscPeso = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabelPreco = new javax.swing.JLabel();
        jLabelTotal = new javax.swing.JLabel();
        jButtonConfirmar = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        jLabelEspecie = new javax.swing.JLabel();
        jLabelDesconto = new javax.swing.JLabel();
        compraPiscValor = new javax.swing.JLabel();
        jButtonCancelar = new javax.swing.JButton();
        jLabelComprarPeixePiscina = new javax.swing.JLabel();
        compraPiscTotal = new javax.swing.JLabel();
        compraPiscDesconto = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        selList = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        labelValor = new javax.swing.JLabel();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jLabel1.setText("Peso");

        compraPiscPeso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compraPiscPesoActionPerformed(evt);
            }
        });
        compraPiscPeso.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                compraPiscPesoKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                compraPiscPesoKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                compraPiscPesoKeyTyped(evt);
            }
        });

        jLabel2.setText("g");

        jLabelPreco.setText("Preço por quilo  R$");

        jLabelTotal.setText("Valor Total");

        jButtonConfirmar.setText("Confirmar");
        jButtonConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmarActionPerformed(evt);
            }
        });

        jLabel15.setText("Valor");

        jLabelEspecie.setText("Espécie");

        jLabelDesconto.setText("Desconto");

        compraPiscValor.setText("0.00");

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseClickedCancelarHandler(evt);
            }
        });

        jLabelComprarPeixePiscina.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabelComprarPeixePiscina.setText("Compra de Peixe da Piscina");

        compraPiscTotal.setText("0.00");

        compraPiscDesconto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                compraPiscDescontoKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                compraPiscDescontoKeyReleased(evt);
            }
        });

        selList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                selListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(selList);

        labelValor.setText("0.00");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(117, 117, 117)
                .addComponent(jLabelEspecie, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                .addGap(424, 424, 424)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonConfirmar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15)
                            .addComponent(jLabelDesconto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(compraPiscValor)
                            .addComponent(compraPiscDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(compraPiscTotal))))
                .addGap(21, 21, 21))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabelPreco)
                                        .addGap(18, 18, 18)
                                        .addComponent(labelValor, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(29, 29, 29)
                                        .addComponent(compraPiscPeso, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel2))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(119, 119, 119)
                                .addComponent(jLabel3))))
                    .addComponent(jLabelComprarPeixePiscina, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 562, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(236, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jLabelComprarPeixePiscina, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabelEspecie)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(compraPiscPeso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabelPreco)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(labelValor))
                                .addGap(108, 108, 108))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(17, 17, 17))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel15)
                            .addComponent(compraPiscValor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelDesconto)
                                .addGap(47, 47, 47)
                                .addComponent(jLabelTotal))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(compraPiscDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(compraPiscTotal)))
                        .addGap(46, 46, 46)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirmar)
                    .addComponent(jButtonCancelar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void mouseClickedCancelarHandler(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mouseClickedCancelarHandler
        // TODO add your handling code here:
        menuPadrao.trocaPanel2(menuPadrao.getMenuPrincipal());
    }//GEN-LAST:event_mouseClickedCancelarHandler

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        refreshList();
    }//GEN-LAST:event_formComponentShown

    private void selListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_selListValueChanged
        Integer NUMERO_SERVICO = (Integer) listaPeixes.get(selList.getSelectedIndex()).get(numServ);
        val = conector.pegaValorPeixePiscina(NUMERO_SERVICO);
        labelValor.setText(Float.toString(val));
    }//GEN-LAST:event_selListValueChanged

    private void compraPiscPesoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_compraPiscPesoKeyPressed
       // System.out.println("keyPressed");
        calcula();
    }//GEN-LAST:event_compraPiscPesoKeyPressed

    private void compraPiscPesoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compraPiscPesoActionPerformed
       // System.out.println("keyPressed");
        calcula();
    }//GEN-LAST:event_compraPiscPesoActionPerformed

    private void compraPiscPesoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_compraPiscPesoKeyReleased
       // System.out.println("keyReleased");
        calcula();
    }//GEN-LAST:event_compraPiscPesoKeyReleased

    private void compraPiscPesoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_compraPiscPesoKeyTyped

    }//GEN-LAST:event_compraPiscPesoKeyTyped

    private void compraPiscDescontoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_compraPiscDescontoKeyPressed
        calcula();        // TODO add your handling code here:
    }//GEN-LAST:event_compraPiscDescontoKeyPressed

    private void compraPiscDescontoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_compraPiscDescontoKeyReleased
        calcula();        // TODO add your handling code here:
    }//GEN-LAST:event_compraPiscDescontoKeyReleased

    private void jButtonConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmarActionPerformed
        if(conector.cadastrarCompraPeixePiscina(valorTotal, peso, listaPeixes.get(selList.getSelectedIndex())))
            JOptionPane.showMessageDialog(this, "Venda realizada com sucesso333333333", "Atenção", JOptionPane.WARNING_MESSAGE, iconeOK);
        else
            JOptionPane.showMessageDialog(this, "Não foi possível realizar a venda", "Atenção", JOptionPane.WARNING_MESSAGE, iconeERRO);
    }//GEN-LAST:event_jButtonConfirmarActionPerformed

    public void calcula() {
        peso = Float.parseFloat(compraPiscPeso.getText().length() > 0 ? compraPiscPeso.getText() : "0");
        float desconto = Float.parseFloat(compraPiscDesconto.getText().length() > 0 ? compraPiscDesconto.getText() : "0");
        float valorFinal = (val * peso) / 1000;
        valorTotal = valorFinal - desconto;
        compraPiscValor.setText(Float.toString(valorFinal));
        compraPiscTotal.setText(Float.toString(valorTotal));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField compraPiscDesconto;
    private javax.swing.JTextField compraPiscPeso;
    private javax.swing.JLabel compraPiscTotal;
    private javax.swing.JLabel compraPiscValor;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonConfirmar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelComprarPeixePiscina;
    private javax.swing.JLabel jLabelDesconto;
    private javax.swing.JLabel jLabelEspecie;
    private javax.swing.JLabel jLabelPreco;
    private javax.swing.JLabel jLabelTotal;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelValor;
    private javax.swing.JList selList;
    // End of variables declaration//GEN-END:variables

     void refreshList() {


        try {
            listaPeixes = (List<Map<String,?>>) conector.doRawSql("SELECT * FROM SERVICOS_DISPONIVEIS_PISCINA");
        } catch (Exception ex) {
            Logger.getLogger(MenuCadastrarEditarServicoPesca.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
          if (listaPeixes == null || listaPeixes.size() == 0)
              return;
          String[][] dados = getListData();
          //set list for JList
          selList.setListData(dados);
          //create Renderer and dislpay
          System.out.println("colunas: " + dados[0].length);
          selList.setCellRenderer((ListCellRenderer) new MyCellRenderer(dados[0].length));
    }

    private String[][] getListData(){
        String[][] dados = null;
//        String campos = "TURNO, PERIODO, TIPO, VALOR";
        //List<Map<String,?>> listaPeixes = null;
        System.out.flush();
        int line = 0;
        int col = 0 ;


        for(Map<String,?> m : listaPeixes){
            if (dados == null){
                dados = new String[listaPeixes.size()][m.size()];
            }
            System.out.println(m);

            for(String key : m.keySet()){
                System.out.println(key + ":" + m.get(key) + ":" + line + ":" + col);
                dados[line][col++] = m.get(key).toString();
            }
            line++;
            col=0;
        }

        return dados;
    }

        static class MyCellRenderer extends JPanel implements ListCellRenderer {

        ArrayList<JLabel> labels = new ArrayList();
        JLabel left, middle, right;
        int cols;

        MyCellRenderer(int cols) {
            this.cols = cols;
            setLayout(new GridLayout(1, cols));
            for (int i=0; i < cols;i++){
                JLabel label = new JLabel();
                label.setOpaque(true);
                add(label);
                labels.add(label);
            }
        }

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            int i = 0;
            for (JLabel jl : labels) {
                jl.setText(((String[])value)[i++]) ;
                if (isSelected) {
                    jl.setBackground(list.getSelectionBackground());
                    jl.setForeground(list.getSelectionForeground());
                } else {
                    jl.setBackground(list.getBackground());
                    jl.setForeground(list.getForeground());
                }
            }

            setEnabled(list.isEnabled());
            setFont(list.getFont());
            return this;
        }
    }

 
}
